# coding=UTF-8
import os
from utils import doc_util, read_config
import requests

base_path = read_config.base_path
config_path = os.path.join(read_config.data_path, 'common', 'config')

# 深证A

url = 'http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get?type=ABLS_MB&token=70f12f2f4f091e459a279469fe49eca5&filter=(scode='')&st=rn&sr=-1&callback=jQuery112405819049666299806_1612405901533&_=1612405901534'
# 添加请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0',
}


# 修改于 whm 2021-02-04
def add_a_stock(code):
    url = 'http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get?type=ABLS_MB&token=70f12f2f4f091e459a279469fe49eca5&' \
          'filter=(scode=' + code + ')&st=rn&sr=-1&callback=jQuery112405819049666299806_1612405901533&_=1612405901534'
    resp1 = requests.get(url, headers=headers)
    content = str(resp1.content, encoding='utf-8').split('([')[1].split('])')[0]
    content = eval(content)
    if isinstance(content, dict):
        name = content['sname']
    else:
        name = content[0]['sname']
    if code[0:2] == '60' or code[0:2] == '68':
        exchange = '1'
    elif code[0:2] == '00' or code[0:2] == '30':
        exchange = '0'
    else:
        exchange = '-1'

    # 更新文档
    stock_dict = doc_util.get_a_doc_dict(os.path.join(config_path, '0.股票信息'), 'code')
    stock_dict[code] = {}
    stock_dict[code]['name'] = name
    stock_dict[code]['code'] = code
    stock_dict[code]['exchange'] = exchange
    doc_util.gen_a_doc(config_path, '0.股票信息', stock_dict)

    return stock_dict


if __name__ == '__main__':
    print()
    # add_a_stock('600121')
    add_a_stock('002692')
